<table id="notepad"></table>
<fieldset style="width:1065px;border:solid 1px #A4BED4;margin-bottom:10px;margin-top:10px;margin-left:-1px;position:relative;">
    <legend>记事本管理操作</legend>
    <form action="/oa/notepadAdd" id="notepadSubmit">
        <div>
            <input type="hidden" name="id" id="ClickRowID" value="">
            <table style="width:100%;table-layout:fixed;">
                <tbody>
                <tr>
                    <td style="width:100px;">标题：</td>
                    <td style="width:260px;">
                        <input name="title" style="width: 250px;" class="text required" id="title" required="true">
                    </td>
                </tr>
                <tr>
                    <td>内容：</td>
                    <td colspan="3">
                        <textarea name="content" id="content" style="width:442px;padding:0; line-height0px;height:150px;">

                        </textarea>
                    </td>
                </tr>
                <tr></tr>
                <tr>
                    <td>&nbsp;</td>
                    <td >
                        <a href="javascript:void(0)" class="button" id="SaveButton">
                            <span class="button-text">新增</span></a>&nbsp;&nbsp;
                        <a href="javascript:void(0)" class="button" id="ResetButton">
                            <span class="button-text">重置</span>
                        </a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </form>
</fieldset>

<script type="text/javascript">
$(document).ready(function(){

    // 初期化DataGrid
    $('#notepad').datagrid({
        width:1080,
        height:320,
        nowrap: true,
        autoRowHeight: false,
        striped: true,
        url:'/oa/notepadPattern',
        sortName: 'id',
        remoteSort: false,
        queryParams:{},
        idField:'id',
        pagination:true,
        rownumbers:true,
        pageList:[10,15,20],
        frozenColumns:[[
            {field:'ck',checkbox:true},
        ]],
        columns:[[
            {field:'title',title:'标题',width:200},
            {field:'content',title:'内容',width:320},
            {field:'created',title:'创建日期',width:220,rowspan:2,sortable:true,
                sorter:function(a,b){
                    return (a>b?1:-1);
                }
            }
        ]],
        toolbar:
                [{
                    id:'btnadd',
                    text:'新增记事本',
                    iconCls:'icon-add',
                    handler:function(){
                        var rows = $('#notepad').datagrid('getSelections');
                        if( rows.length == 1 ){
                            var rowData = rows[0];
                            $('#ClickRowID').val();
                            $('#title').val(rowData.title);
                            $('#content').val(rowData.coontents);
                            $('#SaveButton span').text('新增');
                        }else{
                            parent.$.dooDialog.alert('您只能复制一条记录，请重新选择......');
                        }
                    }
                },'-',{
                    text:'删除选中记事本',
                    iconCls:'icon-remove',
                    handler:function(){
                        var ids  = "";
                        parent.$.dooDialog.confirm("你确定要删除这个记事本吗？",function(){
                            var rows = $('#notepad').datagrid('getSelections');
                            if( rows.length > 0 ){
                                for(var i=0;i<rows.length;i++){
                                    if(i==0){
                                        ids = rows[i].id;
                                    }else{
                                        ids = ids + "," + rows[i].id;
                                    }
                                }

                                $.post('/oa/notepadDel',{deleted:ids},function(data){
                                    if(data.err == 0){
                                        $("#notepad").datagrid('reload');
                                    }else{
                                        parent.$.dooDialog.alert('记事本删除失败！');
                                    }
                                },'json');
                            }else{
                                parent.$.dooDialog.alert('您还没有选择要删除的记录，请选择......');
                            }
                        });
                    }
                },'-',{
                    text:'编辑选中记事本',
                    iconCls:'icon-edit',
                    handler:function(){
                        var rows = $('#notepad').datagrid('getSelections');
                        if( rows.length == 1 ){
                            var rowData = rows[0];
                            $('#ClickRowID').val(rowData.id);
                            $('#title').val(rowData.title);
                            $('#content').val(rowData.coontents);
                            $('#SaveButton span').text('保存');
                        }else{
                            parent.$.dooDialog.alert('您只能编辑一条记录，请重新选择......');
                        }
                    }
                },'-',{
                    text:'查看记事本',
                    iconCls:'icon-search',
                    handler:function(){
                        var rows = $('#notepad').datagrid('getSelections');
                        if( rows.length == 1 ){
                            var rowData = rows[0];
                           // window.location.href = '/oa/notepadView/?id='+(rowData.id);
                            window.open('/oa/notepadView/?id='+(rowData.id));

                            $('#SaveButton span').text('保存');
                        }else{
                            parent.$.dooDialog.alert('您只能查看一条记录，请重新选择......');
                        }
                    }
                    }],
        onUnselect:function(rowIndex, field, value){
            $('#ClickRowID').val();
            $('#title').val('');
            $('#content').val('');
        }
    });


    // 变更提交
    $('a#SaveButton').click(function(){
        // 数据检测
        var isValid = false;
        if ($("#title").val() == '' || $("#content").val() == '') {
            isValid = true;
            return false;
        }


        // 检测结果
        if(isValid){
            return false
        }

        // 数据提交
        $.post($('form#notepadSubmit').attr('action'),$('form#notepadSubmit').serializeArray(),function(data){
            if(data.err == 0){
                $("#notepad").datagrid('reload');
                $('input.text,input.number,input.count').each(function(){
                    $(this).val('');
                });
                $('input[name="title"]').val('');
                $('input[name="content"]').val('');
                $('#ClickRowRemark').val('');
                parent.$.dooDialog.alert('记事本记录成功！');
                return true;
            }else{
                parent.$.dooDialog.alert(data.err);
                return false;
            }
        },'json');
    });

    // 重置
    $('a#ResetButton').click(function(){
        $('input.text,input.number,input.count').each(function(){
            $(this).val('');
        });

        $('input[name="title"]').val('');
        $('input[name="content"]').val('');
        $('#ClickRowRemark').val('');
    });
});
</script>